package com.zyg.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyg.pojo.Class;
import com.zyg.service.ClassService;
import com.zyg.util.LayData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private ClassService classService;

    @RequestMapping("/tolist")
    public String list() {
        return "class/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public String list(Class class1, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        LayData layData = new LayData();
        QueryWrapper<Class> qw=new QueryWrapper<>();
        if(class1.getClassName()!=null && !"".equals(class1.getClassName())){
            qw.like("class_name",  class1.getClassName() );
        }
        long count = classService.count(qw);
        layData.setCount((int)count);
        //查询数据
        //Page<Class> page1=new Page<>(page,limit);
        //List<Class> list = classService.list(page1, qw);
        List<Class> list = classService.findAll();
        layData.setData(list);
        layData.setCode(0);
        layData.setMsg("查询成功");
        String data = JSON.toJSONString(layData);
        return data;
    }
}
